package netspy.dateien;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import netspy.*;
import utilities.*;

public class WordBlacklist extends Datei {
	
	String[] words;
	int badWordCount = 0;
	
	Pattern pt;
	Matcher mt;
	
	public WordBlacklist (File file) throws FileNotFoundException, IOException{
		super(file);
		BufferedReader br = IOHelp.getBufferedFileReader(file, "UTF-8");
		
		while(br.readLine() != null)
			badWordCount ++;
		
		words = new String[badWordCount];
		
		br = IOHelp.getBufferedFileReader(file, "UTF-8");
		for(int i = 0; i < badWordCount; i++){
			words[i] = br.readLine();
		}		
		
	}
	
	public void checkEmail(Mail email){
		int matchcounter;
		MailLog ml;
		for(int i = 0; i < badWordCount; i++){
			matchcounter = 0;
			pt = Pattern.compile(words[i], Pattern.CASE_INSENSITIVE);
			mt = pt.matcher(email.getText());
			while(mt.find()){
				email.addSearchResultIndex(mt.start(),mt.end());
				matchcounter ++;
			}
			if(matchcounter > 0){
				ml = (MailLog)Start.filemanager.getDatei(FileManager.DateiFunc.MAIL_LOG);
				ml.logBadWord(email, words[i],matchcounter);
			}
		}
	}
	
	
}
